var click=0;
var un = $.getUrlParam("username");    
$(document).ready(function () {
var un = $.getUrlParam("username");    
get_items(un);

});





$(document).ready(function () {


    $("#searchbtn").click(function (e) { 
        searchfor(e);
    });

    $("#children").click( 
        function(num){
            classearch(1)
        });
    $("#doll").click(  function(num){
        classearch(2)
    });
    $("#baby").click( function(num){
        classearch(3)
    });
    $("#electronic").click(  function(num){
        classearch(4)
    });
    $("#remote").click(  function(num){
        classearch(5)
    });
    $("#IQ").click(  function(num){
        classearch(6)
    });
    $("#outdoor").click( function(num){
        classearch(7)
    });

    $(".hisrecord").click( function(){
        classearch(7)
    });
});



function classearch(num){

    var str='';
    if(num==1){
        str='儿童玩具';
    }else if(num==2){
        str='玩偶';
    }else if(num==3){
        str='婴儿玩具';
    }else if(num==4){
        str='电动玩具';
    }else if(num==5){
        str='遥控玩具';
    }else if(num==6){
        str='益智玩具';
    }else if(num==7){
        str='室外玩具';
    }
    $.ajax({
        type: "GET",
        url: SERVER_PATH + "/product/listbyclass",
        data: {
            "proClass": str
        },
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status) {
                alertBox(result.data.message);
                return false;
            }



            set_items(result.data);
        }
    });

}












function get_items(un) {

    $.ajax({
        type: "GET",
        url: SERVER_PATH + "/product/list",
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status) {
                alertBox(result.data.message);
                return false;
            }
            set_items(result.data,un);
        }
    });
}



function searchfor(e){
    var name = $("#search").val();


    $.ajax({
        type: "GET",
        url: SERVER_PATH + "/product/listbyname",
        data: {
            "name": name
        },
        xhrFields: {withCredentials: true},
        success: function (result) {
            if (result.status) {
                alertBox(result.data.message);
                return false;
            }
            set_items(result.data,un);
        }
    });

}


function set_items(items,un) {
    
    if (!items) {
        return false;
    }
    $(".goods-list").empty();
    $.each(items, function (i, item) {
        // 定义模板
        var div = 
            '<div class="col-sm-3 goods">' +
                '<div class="row goods-link">' +
                    '<a href="item.html"><img src=""/><h4></h4><p><span>0</span>件剩余</p></a>' +
                '</div>' +
                '<div class="row goods-info">' +
                '<div class="col-sm-8">' +
                    '<div class="row price"><b>&yen;<span>0.00</span></b><i></i></div>' +
                    '<div class="row stock"><i> <span> </span> </i></div>' +
                '</div>' +
                '<div class="col-sm-4 bill-button"><a href="#">商品详情</a></div>' +
                '</div>' +
            '</div>';
        
        // 设置数据
        var $obj = $(div);
        
        $obj.find(".goods-link a").attr("href", "item.html?id=" + item.id+"&username="+un);
        $obj.find(".goods-link img").attr("src", item.imageUrl);
        $obj.find(".goods-link span").text(item.proAmount);
        $obj.find(".goods-info .price span").text(item.price);
        $obj.find(".goods-info .price del").text(item.price);
        $obj.find(".goods-info a").attr("href", "item.html?id=" + item.id+"&username="+un);
       
        // 显示内容
        $(".goods-list").append($obj);
    });
}

